package cn.sinata.wanglin.mine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.DialogActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 修改昵称界面
 */
public class AlternicknameActivity extends DialogActivity {
    @BindView(R.id.et_nickname)
    EditText et_nickname;//修改昵称的输入框
    @Override
    protected int setContentLayout() {

        return R.layout.activity_alternickname;
    }

    @Override
    protected void initView() {
        setCancelOnTouchOutside(false);
        Bundle mBundle=getIntent().getExtras();
        if(mBundle!=null){
            String nickname = mBundle.getString("nickname");
            et_nickname.setText(nickname);
            if (!TextUtils.isEmpty(nickname)) {
                et_nickname.setSelection(nickname.length());
            }
        }

    }
    @OnClick({R.id.tv_doalter,R.id.iv_accoutlogin_delete})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_doalter:
                //描述:确认修改
                String nickname= et_nickname.getText().toString().trim();
                if(TextUtils.isEmpty(nickname)){
                    showToast("昵称不能为空");
                    return;
                }
                alterUserinfo(nickname,Constant.ALT_nickName);
                break;
            case R.id.iv_accoutlogin_delete:
                finish();
                break;
        }
    }
    /**
     * 修改用户相关信息，除了标签以外
     * @param nickname
     * @param tag
     */
    private  void alterUserinfo( final String nickname,int tag){
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        Observable<ResultData<JsonObject>> observable = HttpManager.updUser(""+userid,nickname,tag);
        observable.subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        showDialog();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        Intent intent=new Intent();
                        intent.putExtra("nickname",nickname);
                        SharedPreferencesUtils.save(Constant.SP_NICKNAME,nickname);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });

    }
}
